package com.trainoo.payment.mock.controller;

import com.alibaba.fastjson.JSON;
import com.trainoo.payment.mock.config.CallbackUrl;
import com.trainoo.payment.mock.message.BaseResult;
import com.trainoo.payment.mock.message.request.*;
import com.trainoo.payment.mock.message.response.*;
import com.trainoo.payment.mock.service.PaymentService;
import com.trainoo.payment.mock.util.IPUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p> add description here </p>
 *
 * @author tao.zhou
 * @version 1.0
 * @date 2021/12/1 18:32:00
 */
@RestController
public class PaymentController {

    private static final Logger logger = LoggerFactory.getLogger(PaymentController.class);

    private final static String VERSION = "/beta";

    @Resource
    private PaymentService paymentService;

    @PostMapping(value = "/genUrl" + VERSION)
    public BaseResult<QrUrlGenResponse> qrUrlGen(@RequestBody QrUrlGenRequest request,
                                                 HttpServletRequest servletRequest){
        String ip = IPUtil.getIpRequest(servletRequest);
        String callBackUrl = CallbackUrl.getUrl(ip, request.getNotifyUrl());
        logger.info("接收到[{}][qrUrlGen]请求:{}", ip, JSON.toJSONString(request));
        QrUrlGenResponse response = paymentService.qrUrlGen(request, callBackUrl, ip);
        logger.info("请求[qrUrlGen]返回:{}", JSON.toJSONString(response));
        return new BaseResult<>(response);
    }

    @PostMapping(value = "/qrPay" + VERSION)
    public BaseResult<PayResponse> qrPay(@RequestBody PayRequest request,
                                         HttpServletRequest servletRequest){
        String ip = IPUtil.getIpRequest(servletRequest);
        String callBackUrl = CallbackUrl.getUrl(ip, request.getNotifyUrl());
        logger.info("接收到[{}][qrPay]请求:{}", ip, JSON.toJSONString(request));
        PayResponse response = paymentService.qrPay(request, callBackUrl);
        logger.info("请求[qrPay]返回:{}", JSON.toJSONString(response));
        return new BaseResult<>(response);
    }

    @PostMapping(value = "/payStatus" + VERSION)
    public BaseResult<PayStatusResponse> payStatus(@RequestBody PayStatusRequest request,
                                                   HttpServletRequest servletRequest){
        String ip = IPUtil.getIpRequest(servletRequest);
        logger.info("接收到[{}][payStatus]请求:{}", ip, JSON.toJSONString(request));
        PayStatusResponse response = paymentService.payStatus(request);
        logger.info("请求[payStatus]返回:{}", JSON.toJSONString(response));
        return new BaseResult<>(response);
    }

    @PostMapping(value = "/refund" + VERSION)
    public BaseResult<RefundResponse> refund(@RequestBody RefundRequest request,
                                             HttpServletRequest servletRequest){
        String ip = IPUtil.getIpRequest(servletRequest);
        logger.info("接收到[{}][refund]请求:{}", ip, JSON.toJSONString(request));
        RefundResponse response = paymentService.refund(request);
        logger.info("请求[refund]返回:{}", JSON.toJSONString(response));
        return new BaseResult<>(response);
    }

    @PostMapping(value = "/refundStatus" + VERSION)
    public BaseResult<RefundStatusResponse> refundStatus(@RequestBody RefundStatusRequest request,
                                                         HttpServletRequest servletRequest){
        String ip = IPUtil.getIpRequest(servletRequest);
        logger.info("接收到[{}][refundStatus]请求:{}", ip, JSON.toJSONString(request));
        RefundStatusResponse response = paymentService.refundStatus(request);
        logger.info("请求[refundStatus]返回:{}", JSON.toJSONString(response));
        return new BaseResult<>(response);
    }

    @RequestMapping(value = "/doPay" + VERSION)
    public String doPay(HttpServletRequest servletRequest){
        String orderNo = servletRequest.getParameter("orderNo");
        String callbackUrl = servletRequest.getParameter("callbackUrl");
        String ip = IPUtil.getIpRequest(servletRequest);
        logger.info("接收到[{}][doPay]请求:{}", ip, orderNo);
        paymentService.doPay(orderNo, callbackUrl);
        logger.info("请求[doPay]返回:SUCCESS");
        return "SUCCESS";
    }
}
